yjx.controller('WxController', ['$rootScope', '$scope', '$state', '$compile', 'ipCookie', '$location', '$state', '$timeout', '$fetch', 'LoginService', 'modalService', '$locals', 'wxShareService', 'urlCgService', '$init', 'detailService',
    function WxController($rootScope, $scope, $state, $compile, ipCookie, $location, $state, $timeout, $fetch, LoginService, modalService, $locals, wxShareService, urlCgService, $init, detailService) {
        //console.info(JSON.stringify($location)+'this is WxController');
        var wxState = $location.search().state
        var code = $location.search().code;
        $locals.remove('wxState');
        if (code) {
            if($rootScope.token){
                return;
            }
            if (wxState && wxState.search(/identity\=1/) > 0) {
                $rootScope.identityTo = 1
            } else if (wxState && wxState.search(/inviteId\=/) > 0) {
                $rootScope.identityTo = 1;
                var inviteId = wxState.substring(wxState.indexOf("inviteId=") + 9);
                $rootScope.inviterUserId = parseInt(inviteId);
                // alert("inviteId=="+$rootScope.inviterUserId);
            } else {
                $rootScope.identityTo = 0
            }
            //微信登录code传输
            var wxLoginApi = '/weChat/callback'
            $fetch.fetch(
                wxLoginApi,
                {
                    params: {
                        code: code,
                        identity: $rootScope.identityTo == 1 ? 'manager' : 'customer',
                    },
                    catch: false,
                    type: 'post',
                }
            ).then(function (data) {
                // alert(JSON.stringify(data));
                $init.initLoginData(data);
                ipCookie('token', data.token, ipCookiesParam)
                $locals.set('wxState', wxState);
                var absurl = wxState.split('://')[1];
                // if(wxState.search(/teamNo\=/) > 0){
                //     $rootScope.teamJoin = true;//以小b身份加入团队时
                // }
                $timeout(function () {
                    // $init.initLoginUrl(absurl);
                    $rootScope.identity = $rootScope.identityTo;
                    if (data.state != -1) {
                        $timeout(function () {
                            if ((absurl && absurl.split('/#/') && absurl.split('/#/').length > 1) || (absurl && absurl.split('/') && absurl.split('/').length > 1)) {
                                urlCgService.encodeUrl(absurl);
                            } else {
                                $rootScope.showReady = true;
                                $state.go("index");
                            }
                        }, 500)
                    } else {
                        if($rootScope.teamJoin){
                            urlCgService.encodeUrl(absurl);
                        }else{
                            $rootScope.showReady = true;
                            detailService.openDetail('register');
                        }
                    }

                })
                $timeout(function () {
                    wxShareService.getConfig();
                })
            }).catch(function (e) {
                $location.path($location.path()).search('');
                modalService.mainMsgError(e.msg);
            });
        }
    }]);